﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PS.FlowDesigner;
using pswb.Classes;

namespace pswb.Forms.FlowDesigner
{
    public partial class frmPointSelector : Form
    {
        #region Properties
        public string ActionName { get; private set; }
        private Shape _startPoint;

        public Shape StartPoint
        {
            get { return _startPoint; }
            set
            {
                _startPoint = value;
                lblStartStep.Text = value.Label == null ? value.Tag.ToString() : value.Label.Text;
            }
        }

        private Shape _endPoint;

        public Shape EndPoint
        {
            get { return _endPoint; }
            set
            {
                _endPoint = value;
                lblEndStep.Text = value.Label == null ? value.Tag.ToString() : value.Label.Text;
            }
        }

        #endregion

        public frmPointSelector()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (lblStartStep.Text == "---" || lblEndStep.Text == "---")
            {
                if (lblStartStep.Text == "---")
                    lblStartStep.BackColor = Color.LightSalmon;
                if (lblEndStep.Text == "---")
                    lblEndStep.BackColor = Color.LightSalmon;
                return;
            }
            if (txtActionName.Text == "")
            {
                txtActionName.BackColor = Color.LightSalmon;
                return;
            }
            ActionName = txtActionName.Text;

            frmFlowDesigner parent = this.Owner as frmFlowDesigner;
            parent.AddAction(this);

            this.Close();
        }

        private void txtActionName_TextChanged(object sender, EventArgs e)
        {
            txtActionName.BackColor = Color.White;
        }

        public void SetStep(Shape s, StepType step)
        {
            if (step == StepType.Start)
            {
                if (s.Label == null)
                {
                    if (s.Tag.ToString() == "End")
                    {
                        MessageBox.Show("The final step can't be starting point of an action", "Invalid point", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                StartPoint = s;
            }
            else
            {
                if (s.Label == null)
                {
                    if (s.Tag.ToString() == "Start")
                    {
                        MessageBox.Show("The start step can't be ending point of an action", "Invalid point", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        if (StartPoint != null)
                        {
                            if (StartPoint.Label == null)
                            {
                                if (StartPoint.Tag.ToString() == "Start" && s.Tag.ToString() == "End")
                                {
                                    MessageBox.Show("It is illigal to add a direct action from Start to End.", "Invalid point", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                            }
                        }
                    }
                }
                EndPoint = s;
                txtActionName.Focus();
            }
        }

        private void lblStartStep_TextChanged(object sender, EventArgs e)
        {
            lblStartStep.BackColor = Color.White;
        }

        private void lblEndStep_TextChanged(object sender, EventArgs e)
        {
            lblEndStep.BackColor = Color.White;
        }

        private void frmPointSelector_Load(object sender, EventArgs e)
        {
            lblStartStep.Text = "Click on a step to select";
            lblEndStep.Text = "Control + click to select end step";
        }
    }
}
